package com.duoketech.controller.weixin;

import com.duoketech.config.Constant;
import com.duoketech.controller.BaseController;
import com.duoketech.exception.ValidateException;
import com.duoketech.model.order.Order;
import com.duoketech.service.weixin.cart.WxCartService;
import com.duoketech.service.weixin.order.WxOrderService;
import com.duoketech.utils.pay.weixin.JSSDKInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;

public class WxOrderController extends BaseController {

    @Before(Tx.class)
    public void generatorOrder(){

        boolean result = validateToken(Constant.TOKEN_KEY);
        if(!result){
            renderError("请勿重复提交当前购物车订单信息");
            return;
        }

        int recieverId = getParaToInt("receiverId",0);
        int deliveryType = getParaToInt("deliveryType",0);
        String ids = getPara("ids","");
        int memberId = getMemberId();
        try {
            int id = WxCartService.ME.generatorOrder(memberId,recieverId,deliveryType,ids);
            renderObject(id);
        } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }

    public void payOrderCallBack(){
        int orderId=13;
        WxCartService.ME.payOrderCallBack(orderId,getMemberId());
        renderSuccess("更改状态成功");
    }


    public void remindSendDelivery(){
        int id = getParaToInt("id",0);
        try {
            WxOrderService.ME.sendDelievryMsg(getMemberId(),id);
            renderSuccess("提醒发货成功");
        } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }

    public void confirmOrder(){
        int id = getParaToInt("id",0);
        try {
            WxOrderService.ME.confirmOrder(getMemberId(),id);
            renderSuccess("确认收货成功");
        } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }


    //@Before({JSSDKInterceptor.class})
    public void confirmPayOrder(){
        int id = getParaToInt("id",0);
        Order order = WxOrderService.ME.queryOrderById(id,getMemberId());
        setAttr("order",order);
        render("confirm-pay-order.html");
    }

    public void payOrderSuccess(){
        int id = getParaToInt("id",0);
        render("pay-success.html");
    }


    public void delOrder(){
        int id = getParaToInt("id",0);
        try {
            WxOrderService.ME.delOrder(id,getMemberId());
            renderSuccess("删除成功");
        } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }
}
